pacman::p_load(
tidyverse,sf , rnaturalearth, here
)Mapping with ggplot
Class was inspired by works by Mel Moreno and Mathieu Basile found Here
Introduction to Mapping in R
Overview
Welcome to the “Introduction to Mapping in R” class! This course is designed to provide you with the foundational skills needed to create and visualize geographic data using R.
R is a powerful statistical programming language that offers extensive libraries and tools for working with spatial data, making it an excellent choice for mapping and geographic information system (GIS) applications.
Objectives
By the end of this course, you will be able to: - Understand the basics of spatial data and geographic coordinate systems.
Import and manipulate spatial data in R. Create a variety of maps using popular R packages. Customize maps to highlight specific data insights.
Getting Started
Set Theme
theme_set(theme_bw())The package rnaturalearth provides a map of countries of the entire world. Use ne_countries to pull country data and choose the scale (rnaturalearthhires is necessary for scale = “large”). The function can return sp classes (default) or directly sf classes, as defined in the argument returnclass:
world_df <- ne_countries(scale = "medium", returnclass = "sf")
class(world_df)[1] "sf" "data.frame"
sample(names(world_df),10) [1] "woe_id_eh" "fclass_id" "labelrank" "fclass_eg" "name_tr"
[6] "tiny" "name_pl" "level" "fclass_es" "adm0_a3_il"
Data and basic plot (ggplot and geom_sf)
ggplot(data = world_df) +
geom_sf()Add labels
ggplot(data = world_df) +
geom_sf() +
xlab("Longitude") + ylab("Latitude") +
labs(title = "World map")Add Color
ggplot(data = world_df) +
geom_sf(color = "black", fill = "lightgreen")Filling colors based on a variable
ggplot(data = world_df) +
geom_sf(aes(fill = pop_est))Customising to a color palette - Viridis
ggplot(data = world_df) +
geom_sf(aes(fill = pop_est)) +
scale_fill_viridis_c(option = "mako", trans = "sqrt")Customising to a color palette - Colorbrewer
ggplot(data = world_df) +
geom_sf(aes(fill = pop_est)) +
scale_fill_distiller(palette = 2)Filtering to Africa
world_df |>
filter(continent == "Africa") |>
ggplot() +
geom_sf(aes(fill = pop_est)) +
scale_fill_viridis_c(name = "Population (millions)",
breaks = c(1e6, 10e6, 20e6, 50e6, 100e6, 200e6),
labels = c("1M", "10M", "20M", "50M", "100M", "200M"),
option = "viridis") +
labs(title = "Map of Africa Showing Distribution of Populations") +
theme_void() +
theme(legend.position = "bottom")Explore several filters Views and Facetting
world_df |>
filter(continent %in% c("Africa", "Europe")) |>
ggplot() +
geom_sf()Ugandas Case Study
Load data
uganda_sp <- st_read(here("Shape Files/Districts and Cities/uganda_districts_2019-wgs84.shp"))Reading layer `uganda_districts_2019-wgs84' from data source
`/Users/rutayisiremeddy/Documents/R classes/R-Classes/Mapping/Shape Files/Districts and Cities/uganda_districts_2019-wgs84.shp'
using driver `ESRI Shapefile'
Simple feature collection with 136 features and 9 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 29.57216 ymin: -1.481474 xmax: 35.00105 ymax: 4.231367
Geodetic CRS: WGS 84
Plot basic Map
ggplot(data = uganda_sp)+
geom_sf()Add water bodies
water_sp <- st_read(here("Shape Files/Water Bodies/UGA_water_areas_dcw.shp"))Reading layer `UGA_water_areas_dcw' from data source
`/Users/rutayisiremeddy/Documents/R classes/R-Classes/Mapping/Shape Files/Water Bodies/UGA_water_areas_dcw.shp'
using driver `ESRI Shapefile'
Simple feature collection with 86 features and 5 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 29.64843 ymin: -1.398267 xmax: 34.23037 ymax: 3.436696
Geodetic CRS: WGS 84
ggplot(data = uganda_sp)+
geom_sf()+
geom_sf(data = water_sp)Color the Water Bodies and Basic Styling
ggplot(data = uganda_sp)+
geom_sf()+
geom_sf(data = water_sp,fill = "lightblue")+
theme_void()+
labs(title = "Map of the Uganda", caption = "DataSource: Open_Data_Uganda")